package Test;

import java.util.*;

public class Main {
    // 定义常量
    private static final int STUDENT_NUM = 6;
    private static final int COURSE_NUM = 5;

    // 初始化学生名字数组
    private static final String[] students = {"zhang", "wang", "li", "zhao", "liu", "song"};

    // 初始化课程名称数组
    private static final String[] courses = {"C", "Java", "MySQL", "Linux", "HTML"};

    // 成绩二维数组
    private static final int[][] scores = new int[STUDENT_NUM][COURSE_NUM];

    // 静态初始化块 - 初始化成绩数据
    static {
        // 示例初始化逻辑
        for (int i = 0; i < STUDENT_NUM; i++) {
            for (int j = 0; j < COURSE_NUM; j++) {
                scores[i][j] = (i + 1) * (j + 1) * 10; // 可以替换为实际的成绩值
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String command;
        do {
            System.out.println("请输入命令（格式如：avg Java, avg song, get song Java, sort Java, exit）：");
            command = scanner.nextLine().trim();
            executeCommand(command);
        } while (!command.equalsIgnoreCase("exit"));
        scanner.close();
        System.out.println("查询系统已退出。");
    }

    private static void executeCommand(String command) {
        String[] parts = command.split(" ");
        switch (parts.length > 0 ? parts[0].toLowerCase() : "") {
            case "avg":
                if (parts.length == 2 && isCourse(parts[1])) {
                    printAverageForCourse(parts[1]);
                } else if (parts.length == 2 && isStudent(parts[1])) {
                    printAverageForStudent(parts[1]);
                } else {
                    System.out.println("无效的命令参数，请检查输入。");
                }
                break;
            case "get":
                if (parts.length == 3 && isStudent(parts[1]) && isCourse(parts[2])) {
                    getScore(parts[1], parts[2]);
                } else {
                    System.out.println("无效的命令参数，请检查输入。");
                }
                break;
            case "sort":
                if (parts.length == 2 && isCourse(parts[1])) {
                    sortScoresByCourse(parts[1]);
                } else {
                    System.out.println("无效的命令参数，请检查输入。");
                }
                break;
            default:
                if (!command.equalsIgnoreCase("exit")) {
                    System.out.println("未知命令，请重试。");
                }
                break;
        }
    }

    private static boolean isStudent(String name) {
        return Arrays.asList(students).contains(name);
    }

    private static boolean isCourse(String course) {
        return Arrays.asList(courses).contains(course);
    }

    private static void printAverageForCourse(String course) {
        int index = Arrays.asList(courses).indexOf(course);
        double sum = 0;
        for (int i = 0; i < STUDENT_NUM; i++) {
            sum += scores[i][index];
        }
        System.out.printf("课程 %s 的平均分为: %.2f\n", course, sum / STUDENT_NUM);
    }

    private static void printAverageForStudent(String student) {
        int index = Arrays.asList(students).indexOf(student);
        double sum = 0;
        for (int i = 0; i < COURSE_NUM; i++) {
            sum += scores[index][i];
        }
        System.out.printf("学生 %s 的平均分为: %.2f\n", student, sum / COURSE_NUM);
    }

    private static void getScore(String student, String course) {
        int studentIndex = Arrays.asList(students).indexOf(student);
        int courseIndex = Arrays.asList(courses).indexOf(course);
        System.out.printf("学生 %s 的 %s 成绩为: %d\n", student, course, scores[studentIndex][courseIndex]);
    }

    private static void sortScoresByCourse(String course) {
        int courseIndex = Arrays.asList(courses).indexOf(course);
        List<Integer> scoresList = new ArrayList<>();
        for (int i = 0; i < STUDENT_NUM; i++) {
            scoresList.add(scores[i][courseIndex]);
        }
        scoresList.sort(Comparator.reverseOrder());
        System.out.printf("课程 %s 的成绩排序结果为: %s\n", course, scoresList.toString());
    }
}

/*public class Main {
    public static void main(String[] args) {
        for(char c = 'A'; c <= 'Z'; c++)
        {
            System.out.print(c+" ");
        }
    }
}*/
